﻿using System;
using System.Diagnostics;
using System.IO;

namespace Ldy.Core
{
    /// <summary>
    /// lil 日志功能类 {21EB22A9-1A47-4EDB-BD53-FE030755C7D8}
    /// </summary>
    public class Log
    {
        /// <summary>
        /// 文件路径
        /// </summary>
        private static string FilePath { get; set; } = Sys.GetAppSettings("FilePath") ?? Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Log/" + DateTime.Now.ToString("yyyy-MM-dd"));

        /// <summary>
        /// 文件名称
        /// </summary>
        private static string FileName { get; set; } = Sys.GetAppSettings("FileName") ?? "Trace.log";

        /// <summary>
        /// 文件大小，默认10M
        /// </summary>
        private static long FileSize { get; set; } = Convert.ToInt32(Sys.GetAppSettings("FileSize") ?? "10") * 1024 * 1024;

        /// <summary>
        /// 日志格式，【时间，级别，信息】
        /// </summary>
        private static string LogFormat { get; set; } = Sys.GetAppSettings("LogFormat") ?? "【{0}】={1}=>>>{2}";

        /// <summary>
        /// 日志级别
        /// </summary>

        private static TraceSwitch traceSwitch = new TraceSwitch("TraceLevel", "TraceLevel", "3");

        static Log()
        {
            //创建日志文件路径
            if (!Directory.Exists(FilePath))
                Directory.CreateDirectory(FilePath);
            //设置日志文件名称
            string logFileName = Path.Combine(FilePath, FileName);
            if (Trace.Listeners["Default"] != null)
                ((DefaultTraceListener)Trace.Listeners["Default"]).LogFileName = logFileName;
            Trace.AutoFlush = true;
        }

        /// <summary>
        /// 错误日志
        /// </summary>
        /// <param name="message"></param>
        public static void Error(string message)
        {
            if (traceSwitch.Level >= TraceLevel.Error)
                TraceWrite("ERROR", message);
        }

        /// <summary>
        /// 警告日志
        /// </summary>
        /// <param name="message"></param>
        public static void Warn(string message)
        {
            if (traceSwitch.Level >= TraceLevel.Warning)
                TraceWrite("WARN", message);
        }

        /// <summary>
        /// 信息日志
        /// </summary>
        /// <param name="message"></param>
        public static void Info(string message)
        {
            if (traceSwitch.Level >= TraceLevel.Info)
                TraceWrite("INFO", message);
        }

        /// <summary>
        /// 调试日志
        /// </summary>
        /// <param name="message"></param>
        public static void Debug(string message)
        {
            if (traceSwitch.Level >= TraceLevel.Verbose)
                TraceWrite("DEBUG", message);
        }

        /// <summary>
        /// 万能日志
        /// </summary>
        /// <param name="message"></param>
        public static void Write(string message)
        {
            Trace.WriteLine(string.Format(message));
        }

        /// <summary>
        /// 日志记录
        /// </summary>
        /// <param name="message"></param>
        private static void TraceWrite(string level, string message)
        {
            //创建日志文件路径
            if (!Directory.Exists(FilePath))
                Directory.CreateDirectory(FilePath);
            CheckFileSize();
            string format = string.Format(LogFormat, DateTime.Now, level, message);
            Trace.WriteLine(format);
        }

        /// <summary>
        /// 检查日志文件大小
        /// </summary>
        /// <returns></returns>
        private static void CheckFileSize()
        {
            string logFileName = Path.Combine(FilePath, FileName);
            FileInfo fi = new FileInfo(logFileName);
            if (fi.Exists && fi.Length >= FileSize)
                File.Move(logFileName, Path.Combine(FilePath, FileName + "." + DateTime.Now.ToString("HHmmss")));
        }
    }
}
